package com.sensePlusVR.activity.mine;

import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sensePlusVR.activity.guesture.GestureEditActivity;
import com.sensePlusVR.activity.guesture.GestureVerifyActivity;
import com.sensePlusVR.activity.user.ResetPwdActivity;
import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.bean.AppVersionInfo;
import com.sensePlusVR.main.R;
import com.sensePlusVR.net.NetRequestManager;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.DialogUtils;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MyPageJump;
import com.sensePlusVR.utils.MySharedPreferences;
import com.sensePlusVR.utils.MyToast;
import com.sensePlusVR.utils.PerformancetTestUtil;
import com.sensePlusVR.utils.VersionUtil;
import com.sensePlusVR.widget.dialog.AppVersionTipDialog;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * 
 * @author: Loren
 * @create: 2016-8-10下午2:41:46
 * @desc: My-->Setting界面
 *
 */
@ContentView(R.layout.activity_setting_core)
public class CoreSettingActivity extends BaseActivity implements OnClickListener{
	private static final String TAG = CoreSettingActivity.class.getSimpleName();
	@ViewInject(R.id.title_bar_left_ib)
	private ImageButton title_bar_left_ib;
	
	@ViewInject(R.id.title_bar_title_tv)
	private TextView title_bar_title_tv;
	
	@ViewInject(R.id.setting_core_guest_pwd_relative)
	private RelativeLayout setting_core_guest_pwd_relative;
	
	@ViewInject(R.id.setting_core_replace_icon_relative)
	private RelativeLayout setting_core_replace_icon_relative;
	
	@ViewInject(R.id.setting_core_clear_cache_relative)
	private RelativeLayout setting_core_clear_cache_relative;
	
	@ViewInject(R.id.setting_core_checkupdate_relative)
	private RelativeLayout setting_core_checkupdate_relative;
	
	@ViewInject(R.id.setting_core_detect_relative)
	private RelativeLayout setting_core_detect_relative;
	
	@ViewInject(R.id.setting_version_iv)
	private TextView setting_version_iv;
	
	@ViewInject(R.id.setting_update_point)
	private ImageView setting_update_point;
	
	@ViewInject(R.id.setting_core_pwd_relative)
	private RelativeLayout setting_core_pwd_relative;
	
	@ViewInject(R.id.setting_core_loginout)
	private Button setting_core_loginout;
	
	//版本信息
	private AppVersionInfo newAppVersionInfo;
	
	//是否可以更新
	private boolean canUpdateApp = false;
	//当前版本号
	private String curVersionName;
	
	@Override
	public void setMyContentView() {
		x.view().inject(this);
	
	}

	@Override
	public void initView() {
		super.initView();
		AppManager.getInstance().addActivity(this);
		title_bar_title_tv.setText(R.string.setting_settings);
		
		curVersionName = VersionUtil.getVersionName(this);
		setting_version_iv.setText(curVersionName);
		newAppVersionInfo=((VRApplication)getApplication()).getNewVersionInfo();
		if(newAppVersionInfo!=null){
			canUpdateApp = VersionUtil.canVersionUpdate(newAppVersionInfo.getVersonname(), curVersionName);	
			MyLog.d(TAG, "newAppVersion="+newAppVersionInfo.getVersonname()+" curVersion="+curVersionName+" canUpdateApp="+canUpdateApp);
		}
		setting_update_point.setVisibility(canUpdateApp?View.VISIBLE:View.INVISIBLE);
		
		title_bar_left_ib.setOnClickListener(this);
		setting_core_guest_pwd_relative.setOnClickListener(this);
		setting_core_replace_icon_relative.setOnClickListener(this);
		setting_core_clear_cache_relative.setOnClickListener(this);
		setting_core_checkupdate_relative.setOnClickListener(this);
		setting_core_pwd_relative.setOnClickListener(this);
		setting_core_detect_relative.setOnClickListener(this);
		setting_core_loginout.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_bar_left_ib:
			onBackPressed();
			break;
		case R.id.setting_core_guest_pwd_relative:
			String inputCode = MySharedPreferences.getString2(this,MySharedPreferences.NAME_NOT_CLEAR,Constant.SP_INPUTCODE);
			Log.d(TAG, "input code ： " + inputCode);
			if(inputCode != ""){
				MyPageJump.pageJumpAddBoolean(this, GestureVerifyActivity.class, Constant.INTENT_GUESTURE_FIRST_RESET,true);
			}else{
				MyPageJump.pageJump(this, GestureEditActivity.class);
			}
			break;
		case R.id.setting_core_replace_icon_relative:
			MyPageJump.pageJump(this, ReplaceIconActivity.class);
			break;
		case R.id.setting_core_clear_cache_relative:
			MyPageJump.pageJump(this, ClearCacheActivity.class);
			break;
		case R.id.setting_core_checkupdate_relative:	
			if(canUpdateApp){
				AppVersionTipDialog appVersionTipDlg = new AppVersionTipDialog(this,newAppVersionInfo,false,false);
				appVersionTipDlg.show();
			}else{
				MyToast.showMessage(this,R.string.version_is_latest);
			}	
			break;
		case R.id.setting_core_pwd_relative:
			MyPageJump.pageJump(this, ResetPwdActivity.class);
		break;
		case R.id.setting_core_detect_relative:
			PerformancetTestUtil.startPerformancetTesting(this, Constant.GAME_MODE_VR,true);
			break;
		case R.id.setting_core_loginout:
			exitUserLogin();
			break;
		}
	}

	/**
	 * 退出用户登录
	 */
	private void exitUserLogin() {
		DialogUtils.showConfirmDialogConfirmCancel(this, getResources().getString(R.string.app_loginout), getResources().getString(R.string.system_alert_quit),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						NetRequestManager
								.startLoginOut(CoreSettingActivity.this);
					}
				}, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		MyLog.e(TAG, "CoreSettingActivity pause");
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		MyLog.e(TAG, "CoreSettingActivity onResume");
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		AppManager.getInstance().finishActivity(this);
	}
	
	
}
